package com.gluck.cases.boostrap.interceptor;

import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.exception.BizSerException;
import com.gluck.cases.common.utils.log.BaseLogInterceptor;
import com.gluck.cases.common.utils.log.LoggerNames;
import com.gluck.cases.common.utils.utils.BizResult;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName AuthLogInterceptor
 * @Desc TODO
 * @Author I am Gluck
 * @Date 2022/5/2 - 1:07 上午
 **/
public class ControllerLogInterceptor extends BaseLogInterceptor {

    /**
     * 日志详情
     */
    private static final Logger CONTROLLER_LOG = LoggerFactory.getLogger(LoggerNames.CONTROLLER_LOG);

    /**
     * 日志摘要
     */
    private static final Logger CONTROLLER_LOG_DIGEST = LoggerFactory.getLogger(LoggerNames.CONTROLLER_LOG_DIGEST);

    private static final String SUCCESS = "Y";

    private static final String FAIL = "F";

    private static final String SPLIT = ",";

    private static final String MS = "ms";

    @Override
    public Object bussinessInvoke(MethodInvocation invocation) throws Throwable {
        // 请求参数
        final String args = buildArguments(invocation.getArguments());
        // 请求类和方法
        final String accessClassMethod = buildAccessName(invocation);
        final long startTime = System.currentTimeMillis();
        Object result = BizResult.newResult();
        try{
            CONTROLLER_LOG.info(accessClassMethod + "输入参数={}", args);
            result = invocation.proceed();
            // 日志摘要
            logDigest(accessClassMethod, true, startTime, null, null);
            CONTROLLER_LOG_DIGEST.info(accessClassMethod + "输出结果={}", result);
            return result;
        }catch (BizSerException e){
            result = BizResult.newFailResult(e.getErrCode(), e.getErrMsg());
            logException(accessClassMethod, System.currentTimeMillis() - startTime, e);
            return result;
        }catch (Exception e){
            result = BizResult.newFailResult(BizSerErrorEnum.SYSTEM_ERROR.getGluckStockErrorCode(), e.getMessage());
            logException(accessClassMethod, System.currentTimeMillis() - startTime, e);
            return result;
        }finally {
            // todo
            System.out.println(111);
        }
    }

    private void logException(String accessClassMethod, long startTime, Exception e){
        if(e instanceof BizSerException){
            logDigest(accessClassMethod, false, startTime, ((BizSerException) e).getErrCode(),((BizSerException) e).getErrMsg());
        }else{
            CONTROLLER_LOG.error(accessClassMethod, e);
            logDigest(accessClassMethod, false, startTime, e.getLocalizedMessage(),e.getMessage());
        }
    }

    /**
     * 日志摘要
     * @param accessClassMethod
     * @param result
     * @param startTime
     * @param errCode
     * @param errMsg
     */
    private void logDigest(String accessClassMethod, boolean result, long startTime, String errCode, String errMsg) {
        CONTROLLER_LOG_DIGEST.info(accessClassMethod + SPLIT + (result ? SUCCESS : FAIL) + SPLIT + startTime + MS + SPLIT + errCode + SPLIT + errMsg);
    }
}
